package compare;

import java.util.Arrays;

class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        if (this.age>o.age){
            return 1;
        }else if (this.age==o.age){
            return 0;
        } else {
            return -1;
        }
        //return this.age-o.age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Test {
    public static void main(String[] args) {
        Student[] students=new Student[]{
                new Student("zhansan",18),
                new Student("man",24),
                new Student("lebron",23)
        };
        mySort(students);
        System.out.println(Arrays.toString(students));
    }

    //升序
    private static void mySort(Student[] students) {
        for (int i = 0; i < students.length-1; i++) {
            for(int j=0;j< students.length-1-i;j++){
                if (students[j].compareTo(students[j+1])>0){
                    Student tmp = students[j];
                    students[j]=students[j+1];
                    students[j+1]=tmp;
                }
            }
        }
    }

    public static void main1(String[] args) {
        Student s1 = new Student("lbj",40);
        Student s2 = new Student("xzt",21);
        System.out.println(s1.compareTo(s2));

    }
}
